package com.example.design.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 具体被观察者
 * @author hej
 * @date 2022/11/4 16:19
 */
public class WeatherData implements Subject{
    private List<Observer> observers;

    /**
     * 气温，气压，湿度
     */
    private float temperature, pressure, humidity;

    public WeatherData() {
        this.observers = new ArrayList<>();
    }

    public void setWeatherData(float temperature, float pressure, float humidity) {
        this.temperature = temperature;
        this.pressure = pressure;
        this.humidity = humidity;
        // 模拟展示
        notifyObserver();
    }
    @Override
    public void registerObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        if(observers.contains(observer)){
            observers.remove(observer);
        }
    }

    @Override
    public void notifyObserver() {
        observers.forEach(e -> {
            e.update(temperature,pressure,humidity);
        });
    }
}
